﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CoreRay.Integrators
{
    public class WhittedIntegrator : SurfaceIntegrator
    {
        public WhittedIntegrator(Scene scene) : base(scene)
        {
        }

        //Vector3 lightPosition = new Vector3(-2, 1, -1).Normalize();
        Vector3 lightPosition = new Vector3(-1, 0.5f, -1.5f).Normalize();

        public override Spectrum LI(Ray ray, Intersection primaryIntersection)
        {
            Spectrum spectrum = Spectrum.Black;

            Intersection intersection = primaryIntersection;
            
            if(intersection == null)
            {              
                //TODO: Set Image alpha value to 0.0f
            }

            else 
            {
                Spectrum ambiantSpectrum = Spectrum.FromColor(new Color(0.1f, 0.1f, 0.1f));
                
                spectrum = ambiantSpectrum;
                bool isShadow = false;

                if (intersection.Ray.Direction != Vector3.ZeroVector)
                {
                    Ray shadowRay = new Ray(intersection.DifferentialGeometry.Position, lightPosition);
                    isShadow = this.Scene.IntersectShadow(shadowRay);
                }

                if (isShadow == false)
                {
                    float value = Math.Max(lightPosition * intersection.DifferentialGeometry.Normal, 0.0f);

                    Spectrum colorSpectrum = Spectrum.FromColor(new Color(1.0f, 1.0f, 1.0f));

                    spectrum += colorSpectrum * value;
                }

                //TODO: Set Image alpha value to 0.0f
            }

            return spectrum;
        }
    }
}
